package com.example.defaultshop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CardArrayAdapter extends BaseAdapter {
	private Context context;
	
	private Card card;
	
	private int size;
	
	public CardArrayAdapter(Context context, Card card, int size) {
		this.context = context;
		this.card = card;
		this.size = size;
	}

	@Override
	public int getCount() {
		if (card == null)
			return 10;
		//int n = ((size / card.getAmount()) + (size % (card.getAmount() - 1) == 0 ? 0 : 1)) * card.getAmount();
		int n = (size / (card.getAmount() - 1)) + (size % (card.getAmount() - 1) == 0 ? 0 : 1);
		return (n <= 0 ? card.getAmount() : n * card.getAmount());
	}
	
	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	static class ViewHolder {
		protected ImageView image;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		 
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.item_card, parent, false);
			final ViewHolder viewHolder = new ViewHolder();
			
			viewHolder.image = (ImageView) view.findViewById(R.id.imgStamp);
			view.setTag(viewHolder);
		} else {
			view = (View) convertView;
		}
		
		ViewHolder holder = (ViewHolder) view.getTag();
		int n = size / (card.getAmount() - 1);
		if (position % card.getAmount() == card.getAmount() - 1)
			holder.image.setImageResource((position < size + n ? R.drawable.stempel_gratis_ja : R.drawable.stempel_gratis_nej));
		else
			holder.image.setImageResource((position < size + n ? R.drawable.stempel_ja : R.drawable.stempel_nej));
		return view;
	}
}